﻿#pragma once

#include <cstddef>
#include <functional>
#include <mutex>

namespace asycipc {

class BackpressureController {
 public:
  BackpressureController(std::size_t high_watermark,
                         std::size_t low_watermark);

  void SetHandler(std::function<void(bool)> handler);
  void Update(std::size_t current_bytes);
  bool IsBackpressured() const;

  std::size_t high_watermark() const noexcept { return high_watermark_; }
  std::size_t low_watermark() const noexcept { return low_watermark_; }

 private:
  std::size_t high_watermark_;
  std::size_t low_watermark_;
  mutable std::mutex mutex_;
  std::function<void(bool)> handler_;
  bool backpressured_{false};
};

}  // namespace asycipc
